
import router from '@/router'
import store from '@/store'
// import VueRouter from 'vue-router'

// // to:去哪个页面,
// // const router = new VueRouter({ ... })

// window.localStorage.setItem('storageAside', '0')
// router.beforeEach(async(to, from, next) => {
// //   console.log('to', to)
// //   console.log('from', from)
// //   console.log('next', next)
// //   console.log('store', store.state.user.token)
//   // const token = store.state.user.token
//   // 设置白名单

router.beforeEach(async(to, from, next) => {
  const white = ['/login', '/404', '/register']
  if (store.state.user.token) {
    if (to.path === '/login' || to.path === '/register') {
      next('/')
    } else {
      if (!store.state.user.userInfo.id) {
        // const menu = store.state.user
        // console.log('menu', menu)
        await store.dispatch('user/getRoleDetail')
        /* 获取菜单和按钮权限 */
        await store.dispatch('permission/updateMenu')
        /* 过滤路由 */
        const newRoutes = await store.dispatch('permission/updateRoute')
        // console.log('newRoutes', newRoutes)
        router.addRoutes([...newRoutes, { path: '*', redirect: '/404', hidden: true }])
        next(to.path)
      }
      next()
    }
  } else {
    if (white.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
